package com.candy.candylog.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.candy.candylog.business.entity.LogLink;
import com.candy.candylog.business.mapper.LogLinkMapper;
import com.candy.candylog.business.service.ILogLinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author suyanlong
 * @since 2022-10-25
 */
@Slf4j
@Service
public class LogLinkServiceImpl extends ServiceImpl<LogLinkMapper, LogLink> implements ILogLinkService {

    @Override
    public void saveOrUpdateData(LogLink logLink) {
        if (StringUtils.isEmpty(logLink.getResponseData())) {
            log.info("【{}】保存", logLink.getId());
            this.save(logLink);
        } else {
            if (this.updateById(logLink)) {
                log.info("【{}】更新", logLink.getId());
            } else {
                log.info("【{}】延迟更新", logLink.getId());
                try {
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (Optional.ofNullable(logLink.getNum()).isPresent()) {
                    logLink.setNum(logLink.getNum() + 1);
                } else {
                    logLink.setNum(1);
                }
                if (logLink.getNum() > 20) {
                    return;
                }
                saveOrUpdateData(logLink);
            }
        }
    }

}
